package com.yellowcong.demo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
 * 创建日期:2018年1月11日<br/>
 * 创建时间:下午7:09:34<br/>
 * 创建者 :yellowcong<br/>
 * 机能概要:
 */
public class Demo {

	public static void main(String[] args) throws Exception {
		String hostname = "192.168.100.10";
		String username = "root";
		String password = "yellowcong";

		Connection conn = new Connection(hostname);
		conn.connect();
		boolean isAuthenticated = conn.authenticateWithPassword(username, password);
		
		if(isAuthenticated){
			Session sess = conn.openSession();

        	//所执行的命令
        	//执行命令
        	sess.execCommand("ip  addr");
        	
        	int state = sess.getState();
        	System.out.println(state);
        	//获取读取的信息
        	InputStream in = new StreamGobbler(sess.getStdout());
        	BufferedReader reader =new BufferedReader( new InputStreamReader(in));	
        	
        	String line = null;
        	while((line = reader.readLine()) != null) {
        		System.out.println(line);
        	}
        	in.close();
        	reader.close();
		}
		conn.close();

	}
}
